{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 使用基于最小错误率的Bayes分类器进行仿真\n",
    "已经三个类别分别为：\n",
    "$$\n",
    "\\omega_1:[0, 0], [2, 1], [1, 0];\n",
    "\\omega_2:[-1, 1], [-2, 0], [-2, -1];\n",
    "\\omega_3:[0, -2], [0, -1], [1, -2];\n",
    "$$\n",
    "画出三类的分界线，以及判断样本[-2,2]属于哪一类。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练样本的特征：\n",
      " [[ 0  0]\n",
      " [ 2  1]\n",
      " [ 1  0]\n",
      " [-1  1]\n",
      " [-2  0]\n",
      " [-2 -1]\n",
      " [ 0 -2]\n",
      " [ 0 -1]\n",
      " [ 1 -2]]\n",
      "训练样本的标签: [1 1 1 2 2 2 3 3 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib as mpl\n",
    "\n",
    "# x1,x2,x3分别为训练集资料矩阵\n",
    "x1 = np.array([[0, 0], [2, 1], [1, 0]])\n",
    "x2 = np.array([[-1, 1], [-2, 0], [-2, -1]])\n",
    "x3 = np.array([[0, -2], [0, -1], [1, -2]])\n",
    "y1 = np.array([1]*len(x1))\n",
    "y2 = np.array([2]*len(x2))\n",
    "y3 = np.array([3]*len(x3))\n",
    "# 数据放入统一的训练集中\n",
    "X = np.concatenate((x1,x2,x3),axis=0)\n",
    "y = np.concatenate((y1,y2,y3), axis=0)\n",
    "print(\"训练样本的特征：\\n\",X)\n",
    "print(\"训练样本的标签:\", y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 参数设定\n",
    "eps = 1e-6\n",
    "labels = np.unique(y)\n",
    "K = len(labels)\n",
    "N, M = X.shape\n",
    "\n",
    "parameters = {\n",
    "    \"mean\": np.zeros((K, M)),  # shape: (K, M)\n",
    "    \"sigma\": np.zeros((K, M)),  # shape: (K, M)\n",
    "    \"prior\": np.zeros((K,)),  # shape: (K,)\n",
    "}\n",
    "# 计算各个类别的均值，方差，先验概率\n",
    "for i, c in enumerate(labels):\n",
    "    X_c = X[y == c, :]\n",
    "\n",
    "    parameters[\"mean\"][i, :] = np.mean(X_c, axis=0)\n",
    "    parameters[\"sigma\"][i, :] = np.var(X_c, axis=0) + eps\n",
    "    parameters[\"prior\"][i] = X_c.shape[0] / N"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本属于第[2]类 ，对数概率为： [[-14.98168285  -5.23171548 -43.93222521]]\n"
     ]
    }
   ],
   "source": [
    "def predict(x, labels):\n",
    "    K = len(labels)\n",
    "    log_posterior = np.zeros((x.shape[0], K))\n",
    "    for i in range(K):\n",
    "        mu = parameters[\"mean\"][i]\n",
    "        prior = parameters[\"prior\"][i]\n",
    "        sigma = parameters[\"sigma\"][i]\n",
    "\n",
    "        # log likelihood = log X | N(mu, sigsq)\n",
    "        log_likelihood = -0.5 * np.sum(np.log(2 * np.pi * sigma))\n",
    "        log_likelihood -= 0.5 * np.sum(((x - mu) ** 2) / sigma, axis=1)\n",
    "\n",
    "        log_posterior[:, i] = log_likelihood + np.log(prior)\n",
    "    pred = labels[log_posterior.argmax(axis=1)]\n",
    "    return pred, log_posterior\n",
    "\n",
    "p = np.array([[-2, 2],])\n",
    "pred, log_posterior= predict(p, labels)\n",
    "print(f\"样本属于第{pred}类\", \"，对数概率为：\", log_posterior)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },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xsCXt9DuVqrVU58Bfg6cSrWqSm1q9Z5HxAHAh4AzM9NhzK3Q6j1X7VZSDWeOtQB4sMt9kbrh7VSrM5+Zmet73ZlhkJnfAr4VEVcBH46IL2XmHa1+voShzElFxB4R8ehvspl5J3ANVYlQNWmsfv0acD3w7h53R+qUnwDzI+Jpowci4kBgPs4v04CJiGOB91KtOv5Jr/szyCJiVkTsPebwhUAAT2znXMUHM+Byqr2GAGhs1rYvcFuvOjToGltmfAVYCJzob1kaFI3K+zXAO5oOn0q10vvHPemUVIPGvnFfBb6SmZ+cqr222rOAGyNir6Zj+ze+3tbOiXo+lNmCs4AzIyKpxm1PploheE4vOzXg3gG8gGoTzt2adou+JzNv61mvpM74a+DiiLgEeIhqtdpbMnNDb7sldUZEzKIKZeuBT0fEM5revjEzH+hNzwbaFcDPgG9HxN9SzVv9MPCNzFzWzomKD2aZ+fFGlewUqlVTV1JtvnlLb3s20E5sfB07YfELwGu62xWpszLzkog4Bjgd2Al4XWY6KVqD5BAem+5z2Zj3FgOXdrU3Q6CxHcyLqMLYOcDDwJeZxlSg4nf+lyRJGhb9MMdMkiRpKBjMJEmSCmEwkyRJKoTBTJIkqRAGM0mSpEIYzCRJkgphMJMkSSqEwUySJKkQBjNJkqRCGMwkSZIKYTCTJEkqhMFMkiSpEAYzSZKkQvx/7M8VkTb/O8UAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 720x480 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画图\n",
    "x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1  # 第0列的范围\n",
    "y_min, y_max = X[:, 1].min() -1, X[:, 1].max() + 1  # 第1列的范围\n",
    "xx, yy = np.meshgrid(np.linspace(x_min, x_max, 400), np.linspace(y_min, y_max, 400))  # 生成网格采样点\n",
    "grid_test = np.stack((xx.flat, yy.flat), axis=1)  # 测试点  （xx.flat降维）\n",
    "y_predict, _ = predict(grid_test, labels)\n",
    "\n",
    "cm_pt = mpl.colors.ListedColormap(['r', 'g', 'b'])  # 样本点颜色（样本分为3个类，三个颜色）\n",
    "cm_bg = mpl.colors.ListedColormap(['b', 'y', 'gray'])  # 背景颜色\n",
    "mpl.rcParams['font.sans-serif'] = [u'SimHei']   # 设置字体为SimHei显示中文\n",
    "mpl.rcParams['axes.unicode_minus'] = False  # 设置正常显示字符\n",
    "plt.figure(figsize=(6,4),dpi=120)\n",
    "plt.xlim(x_min, x_max)\n",
    "plt.ylim(y_min, y_max)  # 设置坐标范围\n",
    "plt.pcolormesh(xx, yy, y_predict.reshape(xx.shape), shading='auto', cmap=cm_bg)  # 绘制网格背景\n",
    "plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cm_pt, marker='o')  # 绘制样本点\n",
    "plt.title(u'Bayes分类', fontsize=15)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
